package reduce.fz.com.reduce.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class RankListFragment extends BaseListFragment {
    private Team team;

    public RankListFragment() {
    }
    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();


    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, recyclerView);
    }

    public static RankListFragment create(Team team) {
        RankListFragment fragment = new RankListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("team", team);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.team= (Team) getArguments().getSerializable("team");
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    public interface ItemListener {

        public void itemclick(Long teamid, User user);
    }


    @Override
    protected BaseRecycleViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_rank_view, parent, false);
        RankViewHolder holder = new RankViewHolder(view);
        return holder;
    }

    @Override
    protected BaseRecycleViewHolder createfootViewHolder(View view) {
        return new RankViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List datas, Pagination pagination) {
        String url= ApiUtil.getApi(context,R.array.api_team_user_rank,team.teamid,pagination.getPageNo(),pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }


    public static class RankViewHolder extends BaseRecycleViewHolder<User> {
        private ImageView ivNumber;
        private TextView tvNumber;
        private RoundNetworkImageView ivUserAvatar;
        private TextView tvTicket;
        private ItemListener listener;
        public RankViewHolder(View itemView, ItemListener listener) {
            this(itemView);
            this.listener = listener;
        }


        public RankViewHolder(View itemView) {
            super(itemView);
            ivNumber = (ImageView) itemView.findViewById(R.id.iv_number);
            tvNumber = (TextView) itemView.findViewById(R.id.tv_number);
            ivUserAvatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
            tvTicket = (TextView) itemView.findViewById(R.id.tv_ticket);
            this.listener = listener;
        }

        @Override
        public void renderView(User data, int position) {
            if (position <= 2) {
                ivNumber.setVisibility(View.VISIBLE);
                tvNumber.setVisibility(View.GONE);
            } else {
                ivNumber.setVisibility(View.GONE);
                tvNumber.setVisibility(View.GONE);
            }
            tvNumber.setText(position + "");
            setIVNumber(position);
            ivUserAvatar.setImageUrl(data.avatar, HttpUtils.getAvatarImageLoader());
            tvTicket.setText(data.userAttach.getticket + "");

        }

        public void setIVNumber(int position) {
            switch (position) {
                case 1:
                    ivNumber.setBackgroundResource(R.drawable.diyiming);
                    break;
                case 2:
                    ivNumber.setBackgroundResource(R.drawable.dierming);
                    break;
                case 3:
                    ivNumber.setBackgroundResource(R.drawable.disanming);
                    break;
                default:
                    break;
            }
        }


    }


}
